package com.msr.controller;

import com.msr.pojo.entity.Result;
import com.msr.utils.AliOssUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @author tom
 * @version V1.0
 * @Package com.msr.controller
 * @date 2025/1/28 10:49
 * @Copyright © 株式会社多言语系统研究所
 */
@Tag(name = "文件上传")
@RestController
@RequestMapping("/upload")
public class FileUploadController {
    @Operation(summary = "上传")
    @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<String> upload(@RequestPart("file") MultipartFile  file) throws Exception {
        //获取上传的图片名称
        String fileName = file.getOriginalFilename(); //1.jpg
        //重新生成上传图片名称：UUID fdsfds-fdsfds-fdsfds-fdsfds-fdsfd.jpg
        String imgName = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));  //.jpg
        //实现上传操作
        String imgUrlpath = AliOssUtil.uploadFile(imgName,file.getInputStream());
        return Result.success(imgUrlpath);//https://big-event-gwd.oss-cn-beijing.aliyuncs.com/b5811871-acc8-4583-8399-cf0dc73591ab.png
    }
}
